package com.example.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.admin.domain.TGoods;
import com.example.admin.service.TGoodsService;
import com.example.common.CommonResult;
import com.power.doc.builder.ApiDocBuilder;
import com.power.doc.model.ApiConfig;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/good")
@RefreshScope
public class ConfigController {
    @Autowired
    private TGoodsService goodsService;


    /**
     * 新增/编辑
     *
     * @param goods
     * @return
     */
    @RequestMapping(value = "/saveOrUpdate", method = RequestMethod.POST)
    public CommonResult add(@RequestBody TGoods goods) {
        goodsService.saveOrUpdate(goods);
        return CommonResult.success();
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    public CommonResult delete(@PathVariable(value = "id") Integer id) {
        goodsService.removeById(id);
        return CommonResult.success();
    }

    /**
     * 查询列表
     *
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public CommonResult<List<TGoods>> list() {
        List<TGoods> goods = goodsService.list(new QueryWrapper<TGoods>().eq("delete_flag", 0));
        return CommonResult.success(goods);
    }

    /**
     * 查询详情
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/details/{id}", method = RequestMethod.GET)
    public CommonResult<TGoods> details(@PathVariable(value = "id") Integer id) {
        TGoods goods = goodsService.getById(id);
        return CommonResult.success(goods);
    }

    /**
     * 测试生成文档
     */
    @Test
    public void testBuilderControllersApiSimple() {
        ApiConfig config = new ApiConfig();
        //服务地址
        config.setServerUrl("http://localhost:9000");//接口地址:
        //生成到一个文档
        config.setAllInOne(true);
        //采用严格模式
        config.isStrict();
        //文档输出路径
        config.setOutPath("D:\\");//这里的话 需要自己定义下文件的地址哈
        ApiDocBuilder.buildApiDoc(config);
        //将生成的文档输出到/Users/dujf/Downloads/md目录下，严格模式下api-doc会检测Controller的接口注释
    }
}
